/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientesUIPrincipal.java
 *
 * Created on 08-ene-2012, 19:29:27
 */
package gui.panel.clientes;

import dao.ClienteDao;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import pojos.Cliente;
import util.tableMiniModel;

/**
 *
 * @author !gaso
 */
public class ClientesUIPrincipal extends javax.swing.JPanel {

    private List<Cliente> listClientes = new ArrayList<Cliente>();
    private ClienteDao clienteDAO = new ClienteDao();

    public ClientesUIPrincipal() {
        listClientes = clienteDAO.findAll();
        initComponents();
        
        tableMiniModel t = new tableMiniModel();
        
        
        tablaClientes = t.table(tablaClientes);
        
        
        DefaultTableModel m = new DefaultTableModel();
        
        
        tablaBusqueda = t.table(tablaBusqueda);
        //tablaBusqueda.setColumnModel(tablaClientes.getColumnModel());
        
        
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) tablaClientes.getColumnModel();
        
         List <TableColumn> e = tablaClientes.getColumns();
         Object[] titulos = new Object[e.size()]; 
         for(int i = 0 ; i < e.size() ; i++){
             System.out.println(e.get(i).getHeaderValue());
             titulos[i] = e.get(i).getHeaderValue();
         }
        
         Object[][] dato = new Object[titulos.length][1];
        
       //m.setColumnIdentifiers(titulos);
       m.setDataVector(new Object[titulos.length][1], titulos);
       
      
      
//        
       
       tablaBusqueda.setModel(m);
       
       
        
        //tablaBusqueda = t.AutoSizeColumJX(tablaBusqueda, tablaClientes.getModel());
        
       
       tablaBusqueda.getModel().addTableModelListener(new TableModelListener() {
              public void tableChanged(TableModelEvent e) {
                 System.out.println(e.getColumn() + " | " + tablaBusqueda.getValueAt(0, e.getColumn()) + " | " + tablaBusqueda.getColumnName(e.getColumn()));
                 
                 if (tablaBusqueda.getColumnName(e.getColumn()).toString().equals("Identificacion")){
                     setListClientes(clienteDAO.findByidCliente(tablaBusqueda.getValueAt(0, e.getColumn()).toString()));
                 }else if (tablaBusqueda.getColumnName(e.getColumn()).toString().equals("Tipo Identificacion")){
                     setListClientes(clienteDAO.findBytipoId(tablaBusqueda.getValueAt(0, e.getColumn()).toString()));
                 }
              }
            });
       
     
       
      
      
       
        

    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaClientes = new org.jdesktop.swingx.JXTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaBusqueda = new org.jdesktop.swingx.JXTable();

        jButton7.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton7.setText("Agregar Nuevo Ciente");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jButton8.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton8.setText("Eliminar");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton9.setText("Actualizar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setFont(new java.awt.Font("Helvetica Neue", 0, 14));
        jButton10.setText("Consultar");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listClientes}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, tablaClientes);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipoId.tipoId}"));
        columnBinding.setColumnName("Tipo Identificacion");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idCliente}"));
        columnBinding.setColumnName("Identificacion");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${primerNombre}"));
        columnBinding.setColumnName("Primer Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${segunNombre}"));
        columnBinding.setColumnName("Segundo Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${primerApellido}"));
        columnBinding.setColumnName("Primer Apellido");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${segunApellido}"));
        columnBinding.setColumnName("Segun Apellido");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${direccion}"));
        columnBinding.setColumnName("Direccion");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ciudad.nombreCiudad}"));
        columnBinding.setColumnName("Ciudad");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${EMail}"));
        columnBinding.setColumnName("EMail");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tablaClientes);
        tablaClientes.getColumnModel().getColumn(0).setResizable(false);
        tablaClientes.getColumnModel().getColumn(1).setResizable(false);
        tablaClientes.getColumnModel().getColumn(2).setResizable(false);
        tablaClientes.getColumnModel().getColumn(3).setResizable(false);
        tablaClientes.getColumnModel().getColumn(4).setResizable(false);
        tablaClientes.getColumnModel().getColumn(5).setResizable(false);
        tablaClientes.getColumnModel().getColumn(6).setResizable(false);
        tablaClientes.getColumnModel().getColumn(7).setResizable(false);
        tablaClientes.getColumnModel().getColumn(8).setResizable(false);

        tablaBusqueda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaBusqueda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaBusquedaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tablaBusquedaKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tablaBusquedaKeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(tablaBusqueda);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(313, 313, 313)
                        .addComponent(jButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(417, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton8ActionPerformed

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton9ActionPerformed

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
 this.setListClientes(clienteDAO.findAll());
}//GEN-LAST:event_jButton10ActionPerformed

private void tablaBusquedaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaBusquedaKeyTyped
 

    
    
    
    
}//GEN-LAST:event_tablaBusquedaKeyTyped

private void tablaBusquedaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaBusquedaKeyReleased
    //Indicar que se a Editar la Celdas
    tablaBusqueda.editCellAt(tablaBusqueda.getEditingRow(),tablaBusqueda.getEditingColumn());
}//GEN-LAST:event_tablaBusquedaKeyReleased

private void tablaBusquedaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaBusquedaKeyPressed
}//GEN-LAST:event_tablaBusquedaKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXTable tablaBusqueda;
    private org.jdesktop.swingx.JXTable tablaClientes;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public List<Cliente> getListClientes() {
        return listClientes;
    }

    public void setListClientes(List<Cliente> listClientes) {
        List<Cliente> old=this.listClientes;
        this.listClientes = listClientes;
        firePropertyChange("listClientes", old, listClientes);
    }


}
